﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Ninject;
using BusinessLogic.Interfaces;
using BusinessLogic.Implementations;
using Domain;
using System.Configuration;
using System.Web.Security;
using System.Web.Routing;
namespace Web
{
    public class NinjectControllerFactory : DefaultControllerFactory
    {

        private IKernel ninjectKernel;

        public NinjectControllerFactory()
        {
            ninjectKernel = new StandardKernel();
            AddBinding();
        }

        // извлекаем экземпляр контроллера для заданного контекста запроса и типа контроллера
        protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
        {
            return controllerType == null ? null : (IController)ninjectKernel.Get(controllerType);
        }
        // определяем все привязки
        private void AddBinding()
        {
            ninjectKernel.Bind<IUsersRepository>().To<EFUsersRepository>();
            ninjectKernel.Bind<IProfilesRepository>().To<EFProfilesRepository>();
            ninjectKernel.Bind<IDepartmentsRepository>().To<EFDepartmentsRepository>();
            ninjectKernel.Bind<IRolesRepository>().To<EFRolesRepository>();
            ninjectKernel.Bind<IDishesRepository>().To <EFDishesRepository>();
            ninjectKernel.Bind<IMenusRepository>().To<EFMenusRepository>();
            ninjectKernel.Bind<DomainContext>().ToSelf().WithConstructorArgument("connectionString",
                ConfigurationManager.ConnectionStrings[0].ConnectionString);

            ninjectKernel.Inject(Membership.Provider);
        }
    }
    
}